//
//  QuestionnaireViewController.m
//  ExhibitForcePrototype
//
//  Created by Toma Popov on 12/5/12.
//  Copyright (c) 2012 Dilyana Todorova. All rights reserved.
//

#import "QuestionnaireViewController.h"
#import "CheckoutViewController.h"
static NSInteger counter = 1;
@interface QuestionnaireViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
-(void) okButtonTapped;
@end

@implementation QuestionnaireViewController

@synthesize questionnaireDelegate;
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (counter == 1) {
        [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped)]];
    }
    else
    {
        [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"Question %d",counter] style:UIBarButtonSystemItemCancel target:self action:@selector(backButtonTapped)],[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped)],nil]];
    }
    //NSLog(@"%@",self.navigationItem.backBarButtonItem);
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:counter==3? @"Finish":@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(okButtonTapped)]];
    self.title = [NSString stringWithFormat:@"Question %d",counter];
    self.progressView.progress = (CGFloat)counter/3;
    self.progressLabel.text = [NSString stringWithFormat:@"%d / 3",counter];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark- Action Handlers
-(void)okButtonTapped{
    NSLog(@"ok tapped;");
    if (counter==3) {
        counter =1;
        

        [self.navigationController dismissViewControllerAnimated:YES completion:^(void){
        [self.questionnaireDelegate questionnaireFinished];
        }];
        
        
    }
    else{
    counter++;
    QuestionnaireViewController *viewController = [[QuestionnaireViewController alloc]
                                                   init];
        viewController.questionnaireDelegate= self.questionnaireDelegate;
    [self.navigationController pushViewController:viewController animated:YES];
    }
}
-(void)cancelButtonTapped{
    counter=1;
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void)backButtonTapped
{
    counter--;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark- tableView Data source
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"On which platform(s) would you like to use this application?";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"Select one ore more.";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"defaultCell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellEditingStyleNone];
    }
    
    switch ([indexPath row]) {
        case 0: // First cell in section 1
            cell.textLabel.text = @"iOS";
            break;
        case 1: // Second cell in section 1
            cell.textLabel.text = @"Android";
            break;
        case 2: // Third cell in section 1
            cell.textLabel.text = @" Windows Mobile OS";
            break;
        default:
            // Do something else here if a cell other than 1,2,3 or 4 is requested
            break;
    }
    return cell;
}
#pragma mark - tableView delegate
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:path];
    
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
@end
